package com.mall.controller;


import cn.hutool.core.bean.BeanUtil;
import com.mall.dto.EmpEditPasswordDTO;
import com.mall.dto.EmployeeDTO;
import com.mall.dto.EmployeeLoginDTO;
import com.mall.dto.EmployeePageQueryDTO;
import com.mall.po.Employee;
import com.mall.result.PageResult;
import com.mall.result.Result;
import com.mall.service.EmployeeService;
import com.mall.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author itlxc
 * @since 2024-04-18
 */
@RestController
@RequestMapping("/admin/employee")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    private final EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        EmployeeLoginVO employeeLoginVO = employeeService.login(employeeLoginDTO);
        return Result.success(employeeLoginVO);
    }

    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Result logout() {
        log.info("退出登录");
        return Result.success();
    }

    /**
     * 员工分页查询
     *
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> employeePageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询：{}", employeePageQueryDTO);
        PageResult pageResult = employeeService.employeePageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增员工
     *
     * @param employeeDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增员工")
    public Result saveEmployee(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工: {}", employeeDTO);
        employeeService.saveEmployee(employeeDTO);
        return Result.success();
    }

    /**
     * 根据id查询员工
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工")
    public Result<Employee> getEmpById(@PathVariable Long id) {
        log.info("根据id查询员工: {}", id);
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    /**
     * 编辑员工信息
     *
     * @param employeeDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("编辑员工信息")
    public Result updateEmp(@RequestBody EmployeeDTO employeeDTO) {
        log.info("编辑员工信息: {}", employeeDTO);
        Employee employee = BeanUtil.copyProperties(employeeDTO, Employee.class);
        employeeService.updateById(employee);
        return Result.success();
    }

    /**
     * 启用、禁用员工账号
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("status/{status}")
    @ApiOperation("启用、禁用员工账号")
    public Result StartOrStopEmp(@PathVariable Integer status, Long id) {
        log.info("启用、禁用员工账号: id:{}, status:{}", id, status == 1 ? "启用" : "禁用");
        Employee employee = new Employee();
        employee.setStatus(status);
        employee.setId(id);
        employeeService.updateById(employee);
        return Result.success();
    }

    /**
     * 修改密码
     * @param editPasswordDTO
     * @return
     */
    @PutMapping("/editPassword")
    @ApiOperation("修改密码")
    public Result EmpEditPassword(@RequestBody EmpEditPasswordDTO editPasswordDTO) {
        log.info("修改密码: {}", editPasswordDTO);
        employeeService.EmpEditPassword(editPasswordDTO);
        return Result.success();
    }
}
